package com.blueheart.music.controller;

import com.blueheart.music.common.ResultUtil;
import com.blueheart.music.consts.MyConst;
import com.blueheart.music.exception.BaseExceptionEnum;
import com.blueheart.music.model.pojo.Singer;
import com.blueheart.music.service.SingerService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.*;
import org.springframework.web.multipart.MultipartFile;

import javax.servlet.http.HttpServletRequest;
import java.io.File;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

@RestController
public class SingerController {
    @Autowired
    private SingerService singerService;
    /**
     * 增加歌手
     */
    @PostMapping("/singer/add")
    public ResultUtil addSinger(HttpServletRequest request){
        String name = request.getParameter(MyConst.SING_NAME).trim();
        String sex = request.getParameter(MyConst.SING_SEX).trim();
        String pic = request.getParameter(MyConst.SING_PIC).trim();
        String birth = request.getParameter(MyConst.SING_BIRTH).trim();
        String location = request.getParameter(MyConst.SING_LOCATION).trim();
        String introduction = request.getParameter(MyConst.SING_INTRODUCTION).trim();
        if (StringUtils.isEmpty(name)){
            return  ResultUtil.error(BaseExceptionEnum.NAME_NULL);
        }
        if (singerService.checkName(name)==null){
            //转换生日格式
            SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
            Date date = new Date();
            try {
                date=simpleDateFormat.parse(birth);
            } catch (ParseException e) {
                e.printStackTrace();
            }
            Singer singer = new Singer();
            singer.setName(name);
            singer.setSex(new Byte(sex));
            singer.setBirth(date);
            singer.setIntroduction(introduction);
            singer.setLocation(location);
            Boolean flag = singerService.addSinger(singer);
            if (flag){
                return ResultUtil.success();
            }
            return  ResultUtil.error(BaseExceptionEnum.ADD_ERROR);
        }
        return  ResultUtil.error(BaseExceptionEnum.SINGER_EXIST);
    }
    /**
     * 删除歌手
     */
    @PostMapping("/singer/delete")
    public ResultUtil removeSinger(@RequestParam("id") Integer id){
        if (id > 0) {
            Boolean flag = singerService.removeSinger(id);
            if (flag){
                return ResultUtil.success();
            }
            return ResultUtil.error(BaseExceptionEnum.REMOVE_ERROR);
        }
        return ResultUtil.error(BaseExceptionEnum.PARAM_ERROR);
    }
    /**
     * 更新歌手
     */
    @PostMapping("/singer/update")
    public ResultUtil ReviseSinger(HttpServletRequest request){
        String id = request.getParameter(MyConst.SING_ID).trim();
        String name = request.getParameter(MyConst.SING_NAME).trim();
        String sex = request.getParameter(MyConst.SING_SEX).trim();
        String birth = request.getParameter(MyConst.SING_BIRTH).trim();
        String location = request.getParameter(MyConst.SING_LOCATION).trim();
        String introduction = request.getParameter(MyConst.SING_INTRODUCTION).trim();
        //转换生日格式
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyyy-MM-dd");
        Date date = new Date();
        try {
            date=simpleDateFormat.parse(birth);
        } catch (ParseException e) {
            e.printStackTrace();
        }
//
        Singer singer = new Singer();
        singer.setId(new Integer(id));
        singer.setName(name);
        singer.setSex(new Byte(sex));
        singer.setBirth(date);
        singer.setIntroduction(introduction);
        singer.setLocation(location);
        Boolean flag = singerService.reviseSinger(singer);
        if (flag){
            return ResultUtil.success();
        }
        return  ResultUtil.error(BaseExceptionEnum.UPDATE_ERROR);
    }
    /**
     * 查询全部
     */
    @GetMapping("/singer/showAll")
    public ResultUtil showAllList(){
        List<Singer> all = singerService.findAll();
        if (all.size()>0){
            return ResultUtil.success(all);
        }
        return ResultUtil.error(BaseExceptionEnum.SELETE_ERROR);
    }
    /**
     * 根据名字模糊查询
     */
    @PostMapping("/singer/showSingerByName")
    public ResultUtil showListByName(@RequestParam("name") String name){
        if (StringUtils.isEmpty(name)){
            return  ResultUtil.error(BaseExceptionEnum.NAME_NULL);
        }
        List<Singer> singers = singerService.findByName("%" + name + "%");
        if (singers.size()>0){
            return ResultUtil.success(singers);
        }
        return ResultUtil.error(BaseExceptionEnum.SELETE_ERROR);
    }
    /**
     * 根据性别查询
     */
    @PostMapping("/singer/showSingerBySex")
    public ResultUtil showListBySex(@RequestParam("sex") Byte sex){
        List<Singer> singers = singerService.findBySex(sex);
        if (singers.size()>0){
            return ResultUtil.success(singers);
        }
        return ResultUtil.error(BaseExceptionEnum.SELETE_ERROR);
    }
    /**
     * 更新图片
     */
    @PostMapping("/singer/updatePic")
    public ResultUtil updatePic(HttpServletRequest request){
        String id = request.getParameter(MyConst.SING_ID).trim();
        String pic = request.getParameter(MyConst.SING_PIC).trim();
        Singer singer = new Singer();
        singer.setId(new Integer(id));
        singer.setPic(pic);
        Boolean flag = singerService.reviseSinger(singer);
        if (flag){
            return ResultUtil.success();
        }
        return  ResultUtil.error(BaseExceptionEnum.UPDATE_ERROR);
    }
    /**
     * 通过歌手名查询id
     */
    @PostMapping("/singer/selectToId")
    public  ResultUtil selectToId(@RequestParam("name") String name){
        if (StringUtils.isEmpty(name)){
            return  ResultUtil.error(BaseExceptionEnum.NAME_NULL);
        }
        Singer singer = singerService.findToId(name);
       if (singer!=null){
           return ResultUtil.success(singer);
       }
        return ResultUtil.error(BaseExceptionEnum.SELETE_ERROR);
    }

    /**
     * id查询歌手
     * @param id
     * @return
     */
    @PostMapping("/singer/selectById")
    public ResultUtil selectById(@RequestParam("id") Integer id){
        if (id>0){
            Singer byId = singerService.findById(id);
            if (byId!=null){
                return  ResultUtil.success(byId);
            }
            return  ResultUtil.error(BaseExceptionEnum.SELETE_ERROR);
        }
        return ResultUtil.error(BaseExceptionEnum.PARAM_ERROR);
    }
}

